#!/bin/bash

ROBOTSFILE=RobotFactoryImpl.cpp

echo '' > $ROBOTSFILE
exec > $ROBOTSFILE
echo "#include <string.h>" 
echo "#include \"RobotFactory.h\""
echo "#include \"RobotFactoryImpl.h\""
ROBOTLIST=""
BODYOFCREATE=""
for f in `ls robots/*.h`
do
	echo "#include \"$f\""
	NAME=`echo $f | sed -r 's/^([a-z]+)\/(.+)\.h$/\2/'`
	ROBOTLIST="$ROBOTLIST,$NAME"
	CLASSNAME=`echo $f | sed -r 's/^([a-z]+)\/(.+)\.h$/\1::\2/'`
	BODYOFCREATE="$BODYOFCREATE	if (strcmp(name,\"$NAME\") == 0) {"
	BODYOFCREATE="$BODYOFCREATE		return new $CLASSNAME;}"
	# do something on $f
done
echo "Robot * RobotFactoryImpl::create(const char* name) {"
echo $BODYOFCREATE
echo "}"
echo "char * RobotFactoryImpl::getAvailableRobots() {"
echo "char names[] = \"$ROBOTLIST\";"
echo "char *retnames = new char[strlen(names)+1];"
echo "strcpy(retnames,names);"
echo "return retnames;"
echo "}"


OBSTACLESFILE=ObstacleFactoryImpl.cpp

echo "" > $OBSTACLESFILE
exec > $OBSTACLESFILE
echo "#include <string.h>"
echo "#include \"ObstacleFactory.h\""
echo "#include \"ObstacleFactoryImpl.h\"" 
BODYOFCREATE=""
ROBOTLIST=""
for f in `ls obstacles/*.h`
do
	echo "#include \"$f\"" 
	NAME=`echo $f | sed -r 's/^([a-z]+)\/(.+)\.h$/\2/'`
	ROBOTLIST="$ROBOTLIST,$NAME"
	CLASSNAME=`echo $f | sed -r 's/^([a-z]+)\/(.+)\.h$/\1::\2/'`
	BODYOFCREATE="$BODYOFCREATE if (strcmp(name,\"$NAME\") == 0) {"
	BODYOFCREATE="$BODYOFCREATE return new $CLASSNAME;}"
	# do something on $f
done
echo "Obstacle * ObstacleFactoryImpl::create(const char* name) {"
echo $BODYOFCREATE
echo "}"

echo "char * ObstacleFactoryImpl::getAvailableObstacles() {"
echo "const char* names = \"$ROBOTLIST\";"
echo "char *retnames = new char[strlen(names)+1];"
echo "strcpy(retnames, names);"
echo "return retnames;"
echo "}"
